layui.use(['table', 'admin', 'ax', 'func', 'laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var func = layui.func;

    laydate.render({
        elem: '#betweenMonth'
        ,type: 'month'
        , range: '至' //或 range: '~' 来自定义分割字符
    });

    /**
     * 报销表管理
     */
    var Reimbursement = {
        tableId: "reimbursementTable"
    };

    /**
     * 初始化表格的列
     */
    Reimbursement.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: ''},
            {field: 'occurDate', sort: true, title: '发生日期',templet: function(d){
                    return d.occurDate.substring(0,11);
                }
            },
            {field: 'amount', sort: true, title: '金额（元）',totalRow:true,totalRowText:'合计：'},
            {field: 'memo', sort: true, title: '备注'},
            {field: 'projectName', sort: true, title: '所属项目'},
            {field: 'userName', sort: true, title: '记录人'},
            {field: 'type', sort: true, title: '费用类型'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Reimbursement.search = function () {
        var queryData = {};
        queryData['betweenMonthStr'] = $("#betweenMonth").val();
        queryData['type'] = $("#type").val();
        table.reload(Reimbursement.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    Reimbursement.openAddDlg = function () {
        func.open({
            title: '添加报销表',
            content: Feng.ctxPath + '/reimbursement/add',
            tableId: Reimbursement.tableId
        });
    };

    /**
    * 点击编辑
    *
    * @param data 点击按钮时候的行数据
    */
    Reimbursement.openEditDlg = function (data) {
        func.open({
            title: '修改报销表',
            content: Feng.ctxPath + '/reimbursement/edit?id=' + data.id,
            tableId: Reimbursement.tableId
        });
    };

    /**
     * 导出excel按钮
     */
    Reimbursement.exportExcel = function () {
        var checkRows = table.checkStatus(Reimbursement.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Reimbursement.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/reimbursement/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Reimbursement.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Reimbursement.tableId,
        url: Feng.ctxPath + '/reimbursement/list',
        page: false,
        height: "full-158",
        cellMinWidth: 100,
        cols: Reimbursement.initColumn(),
        totalRow: true,
        where : {betweenMonthStr:$('#betweenMonth').val()}
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Reimbursement.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Reimbursement.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Reimbursement.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + Reimbursement.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Reimbursement.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Reimbursement.onDeleteItem(data);
        }
    });
});
